package box.tuson.com.tusonbox;
import android.os.*;
import android.support.v7.app.*;
import android.widget.*;
import android.view.View.*;
import android.view.*;
import java.util.*;
import org.apache.http.impl.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.client.*;
import java.io.*;
import org.apache.http.*;
import org.apache.http.util.*;
import android.support.design.widget.*;
import android.content.*;

public class dlj extends AppCompatActivity
{  
      
    private Button jiazai;  
    private String apiUrl = "http://suo.im/api.php?url=";     
    Handler handler;   
   private EditText edittext;
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
		//版本4.0后需加这个，不然就报错android.os.NetworkOnMainThreadException
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
								   .detectDiskReads().detectDiskWrites().detectNetwork()
								   .penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
							   .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
							   .penaltyLog().penaltyDeath().build());
        //
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dlj);
        //初始化

        edittext = (EditText) findViewById(R.id.dljEditText1);
        jiazai = (Button) findViewById(R.id.dljButton1);
   


        jiazai.setOnClickListener(new Button.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					String url =apiUrl +edittext.getText().toString();
					getPDAServerData(url);
				}
			});
		FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					Snackbar.make(view, "复制成功～", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
					ClipboardManager fz = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
					edittext = (EditText) findViewById(R.id.dljEditText1);
					fz.setText(edittext.getText());
				}
			});
    }

    public void getPDAServerData(String url) {
        HttpClient client = new DefaultHttpClient();
        HttpPost request;

        try {

            request = new HttpPost(url);

            //调用HttpClient对象的execute(HttpUriRequest request)发送请求，返回一个HttpResponse
            HttpResponse response = client.execute(request);

            //返回响应码为200
            if (response.getStatusLine().getStatusCode() == 200) {

                //从响应中获取消息实体
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String out = EntityUtils.toString(entity);
                    edittext.setText(out);
                }
            }


        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
